package com.itgm.jiayun_web_project.config;

import com.itgm.jiayun_web_project.interceptor.SessionInterceptor;
import com.itgm.jiayun_web_project.interceptor.TokenInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration //配置类
@Slf4j //日志
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private TokenInterceptor tokenInterceptor;

    @Autowired
    private SessionInterceptor sessionInterceptor;

    /**
     * 配置静态资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始配置静态资源映射...");
        registry.addResourceHandler("/back/**")
                .addResourceLocations("classpath:/back/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 自定义Swagger UI访问路径
        // 这里将/swagger路径映射到Swagger UI的默认页面
        registry.addViewController("/swagger")
                .setViewName("forward:/swagger-ui/index.html");

        // 可以添加多个路径映射
        registry.addViewController("/docs")
                .setViewName("forward:/swagger-ui/index.html");
    }

    /**
     * 配置拦截器
     * @param registry
     */

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(sessionInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/login");//不拦截登录接口

//        registry.addInterceptor(tokenInterceptor)
//                .addPathPatterns("/**")
//                .excludePathPatterns("/login");//不拦截登录接口
    }
}
